package me.minecraft.plugin.itemrecycle.config;

import lombok.Getter;
import me.minecraft.plugin.itemrecycle.ItemRecycle;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
 * @author langle__
 * @version 1.0
 */
@Getter
public class MainConfig extends BaseConfig {

    public static final MainConfig INSTANCE = new MainConfig(ItemRecycle.instance, "config.yml");

    private String guiTitle;
    private ItemStack borderItem;
    private ItemBuilder nextPageItem;
    private ItemBuilder previousPageItem;

    public MainConfig(JavaPlugin plugin, String fileName) {
        super(plugin, fileName);
        saveDefault();
    }

    @Override
    public void load() {
        super.load();
        guiTitle = getString("gui.title");
        borderItem = getItemStack("gui.item.border");
        nextPageItem = getItemBuilder("gui.item.next_page");
        previousPageItem = getItemBuilder("gui.item.previous_page");
    }

    public ItemBuilder getItemBuilder(@NotNull String path) {
        Material material = getMaterial(path + ".material");
        String name = getString(path + ".name");
        List<String> lore = getStringList(path + ".lore");
        return new ItemBuilder().material(material).name(name).lore(lore);
    }

    public ItemStack getNextPageItem(int page, int maxPage) {
        ItemStack stack = new ItemStack(nextPageItem.material);
        ItemMeta meta = stack.getItemMeta();
        if (meta != null) {
            meta.setDisplayName(nextPageItem.name.replace("{page}", String.valueOf(page)).replace("{maxPage}", String.valueOf(maxPage)));
            meta.setLore(nextPageItem.lore);
            stack.setItemMeta(meta);
        }
        return stack;
    }

    public ItemStack getPreviousPageItem(int page, int maxPage) {
        ItemStack stack = new ItemStack(previousPageItem.material);
        ItemMeta meta = stack.getItemMeta();
        if (meta != null) {
            meta.setDisplayName(previousPageItem.name.replace("{page}", String.valueOf(page)).replace("{maxPage}", String.valueOf(maxPage)));
            meta.setLore(previousPageItem.lore);
            stack.setItemMeta(meta);
        }
        return stack;
    }

    public static class ItemBuilder {
        private Material material;
        private String name;
        private List<String> lore;

        public ItemBuilder material(Material material) {
            this.material = material;
            return this;
        }

        public ItemBuilder name(String name) {
            this.name = name;
            return this;
        }

        public ItemBuilder lore(List<String> lore) {
            this.lore = lore;
            return this;
        }

    }

}
